/*
 * Camera.cpp
 *
 *  Created on: 2012/07/03
 *      Author: Weihe
 */
#include <cmath>
#include "geom.h"
#include "Camera.h"
#include <gl/glut.h>

#define PI       3.14159265358979323846

namespace glex {

Camera::Camera() :
		calc(true), position(Vector4f()), quad(Quaternion::ZEROS), lens(
				Lens::LENS_50MM), film(Film::FILM_135) {
}

void Camera::setupPerspective(int windowWidth, int windowHeight) const {
	Rectangle windowRect(windowWidth, windowHeight);
	Rectangle rect;
	rect.createInside(windowRect, film.aspect);
	glViewport((int) rect.x, (int) rect.y, (int) rect.width, (int) rect.height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	double aspect = film.aspect;
	double fovy = atan2(film.height / 2, lens.focalLength) * 180 / PI * 2;
	double zNear = 0.1;
	double zFar = 400;
	gluPerspective(fovy, aspect, zNear, zFar);
}

void Camera::updateCamera() const {
	Vector4f focus = position + frontVector();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(position.x, position.y, position.z, focus.x, focus.y, focus.z,
			up.x, up.y, up.z);
}

const float mm = 0.01;

const Lens Lens::LENS_50MM(50 * mm);

Film Film::FILM_135(36 * mm, 24 * mm);
Film Film::FILM_APSC(22.3 * mm, 14.9 * mm);
Film Film::FILM_645(60 * mm, 45 * mm);

} /* namespace glex */
